package monitor;

import config.Config;
import model.HTExtractItem;

import java.sql.Time;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by CatM on 18/1/29.
 */
public class HTMonitorManager {
    private static HTMonitorManager INTANCE = new HTMonitorManager();
    private Timer timer;
    private TimerTask timerTask;

    private HTMonitorManager(){
        timerTask = new TimerTask() {
            @Override
            public void run() {
                try {
                    List<IMonitor> monitors = (List<IMonitor>) getAllMonitor();
                    for (IMonitor monitor : monitors) {
                        List<HTExtractItem> items = monitor.extractItems();
                        List<HTExtractItem> matchItems = monitor.find(items);
                        monitor.notifyUser(matchItems);
                    }
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        timer = new Timer();
    }

    public static HTMonitorManager get(){
        return INTANCE;
    }


    public void start(){
        System.out.println("HTMonitor程序开始");
        System.out.println(">>>>>>>>>>>>>>>>>>>>");
        timer.schedule(timerTask, 0, Config.POLL_TIME);
    }

    private List<? extends IMonitor> getAllMonitor(){
        return Arrays.asList(
                new BCOMonitor(),
                new FiveFiveMonitor(),
                new CanmakeMonitor()

        );
    }
}
